package com.platform.web.controller.base;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.*;
import com.platform.base.service.IBaseLocationService;
import com.platform.base.service.ICollectorInfoService;
import com.platform.base.service.IValveTypeService;
import com.platform.common.core.text.Convert;
import com.platform.common.utils.netty.NettyUtil;
import com.platform.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.platform.common.annotation.Log;
import com.platform.common.enums.BusinessType;
import com.platform.base.service.IValveInfoService;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.poi.ExcelUtil;
import com.platform.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 阀门信息Controller
 * 
 * @author wangqi
 * @date 2020-09-25
 */
@Controller
@RequestMapping("/base/valve/info")
public class ValveInfoController extends BaseController
{
    private String prefix = "base/valve/info";

    @Autowired
    private IValveInfoService valveInfoService;

    @Autowired
    private IValveTypeService valveTypeService;

    @Autowired
    private IBaseLocationService baseLocationService;

    @Autowired
    private ICollectorInfoService collectorInfoService;

    @RequiresPermissions("base:valve:info:view")
    @GetMapping(value = { "", "/{typeId}" })
    public String info(@PathVariable(value = "typeId", required = false) String typeId, ModelMap mmap)
    {
        List<ValveType> types = valveTypeService.list();
        mmap.put("types", types);
        if(typeId==null){
            typeId = "";
        }
        mmap.put("typeId", typeId);
        return prefix + "/info";
    }

    /**
     * 根据采集器查询阀门
     * @param collectorId
     * @param mmap
     * @return
     */
    @RequiresPermissions("base:valve:info:view")
    @GetMapping("/collector/{collectorId}")
    public String collector(@PathVariable(value = "collectorId") String collectorId, ModelMap mmap)
    {
        List<ValveType> types = valveTypeService.list();
        mmap.put("types", types);
        mmap.put("typeId", "");
        mmap.put("collectorId", collectorId);
        return prefix + "/info";
    }

    /**
     * 查询阀门信息列表
     */
    @RequiresPermissions("base:valve:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ValveInfo valveInfo)
    {
        startPage();
        List<ValveInfo> list = valveInfoService.selectValveInfoList(valveInfo);
        return getDataTable(list);
    }

    /**
     * @Author 王琦
     * @Description //阀门信息导入
     * @Date 2020/6/19 19:56
     * @Param [file, updateSupport]
     * @return com.platform.common.core.domain.AjaxResult
     */
    @Log(title = "阀门信息导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("base:valve:info:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ValveInfo> util = new ExcelUtil<ValveInfo>(ValveInfo.class);
        List<ValveInfo> infoList = util.importExcel(file.getInputStream());
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = valveInfoService.importInfo(infoList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * @Author 王琦
     * @Description //导入模板
     * @Date 2020/6/19 19:56
     * @Param []
     * @return com.platform.common.core.domain.AjaxResult
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<ValveInfo> util = new ExcelUtil<ValveInfo>(ValveInfo.class);
        return util.importTemplateExcel("阀门信息");
    }

    /**
     * 导出阀门信息列表
     */
    @RequiresPermissions("base:valve:info:export")
    @Log(title = "阀门信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ValveInfo valveInfo)
    {
        List<ValveInfo> list = valveInfoService.selectValveInfoList(valveInfo);
        ExcelUtil<ValveInfo> util = new ExcelUtil<ValveInfo>(ValveInfo.class);
        return util.exportExcel(list, "阀门信息");
    }

    /**
     * 新增阀门信息
     */
    @GetMapping("/add/{locationId}/{typeId}/{collectorId}")
    public String add(@PathVariable(value = "locationId") String locationId, @PathVariable(value = "typeId") String typeId, @PathVariable(value = "collectorId") String collectorId, ModelMap mmap)
    {
        List<ValveType> types = valveTypeService.list();
        mmap.put("types", types);
        mmap.put("typeId", typeId);
        mmap.put("collectorId", collectorId);
        List<CollectorInfo> infos = collectorInfoService.list();
        mmap.put("infos", infos);
        mmap.put("location", baseLocationService.getById(locationId));
        return prefix + "/add";
    }

    /**
     * 新增保存阀门信息
     */
    @RequiresPermissions("base:valve:info:add")
    @Log(title = "阀门信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated ValveInfo valveInfo)
    {
        valveInfo.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(valveInfoService.insertValveInfo(valveInfo));
    }

    /**
     * 修改阀门信息
     */
    @GetMapping("/edit/{valveId}")
    public String edit(@PathVariable("valveId") String valveId, ModelMap mmap)
    {
        ValveInfo valveInfo = valveInfoService.selectValveInfoById(valveId);
        mmap.put("valveInfo", valveInfo);
        List<ValveType> types = valveTypeService.list();
        mmap.put("types", types);
        List<CollectorInfo> infos = collectorInfoService.list();
        mmap.put("infos", infos);
        return prefix + "/edit";
    }

    /**
     * 修改保存阀门信息
     */
    @RequiresPermissions("base:valve:info:edit")
    @Log(title = "阀门信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ValveInfo valveInfo)
    {
        valveInfo.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(valveInfoService.updateValveInfo(valveInfo));
    }

    /**
     * 删除阀门信息
     */
    @RequiresPermissions("base:valve:info:remove")
    @Log(title = "阀门信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(valveInfoService.deleteValveInfoByIds(ids));
    }

    /**
     * 校验阀门信息是否唯一
     */
    @PostMapping("/checkValveUnique")
    @ResponseBody
    public String checkValveUnique(ValveInfo valveInfo)
    {
        return valveInfoService.checkValveUnique(valveInfo);
    }

    /**
     * 根据地址查询阀门信息
     */
    @PostMapping(value = { "/selectValveByLocation/{locationId}", "/selectValveByLocation/{valveId}/{locationId}" })
    @ResponseBody
    public String selectValveByLocation(@PathVariable(value = "valveId", required = false) String valveId, @PathVariable("locationId") Long locationId)
    {
        ValveInfo valveInfo = valveInfoService.getOne(new QueryWrapper<ValveInfo>().eq("location_id", locationId));
        if(valveInfo!=null){
            if(valveId!=null&&valveId.equals(valveInfo.getValveId())){
                return "";
            }
            return valveInfo.getValveId();
        }else{
            return "";
        }
    }

    /**
     * 阀门登记
     */
    @Log(title = "阀门登记", businessType = BusinessType.INSERT)
    @RequiresPermissions("base:valve:info:register")
    @PostMapping("/register/{valveIds}")
    @ResponseBody
    public AjaxResult register(@PathVariable("valveIds") String valveIds) throws InterruptedException {
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = valveInfoService.registerInfo(Convert.toStrArray(valveIds), operName);
        return AjaxResult.success(message);
    }

    /**
     * 阀门注销
     */
    @Log(title = "阀门注销", businessType = BusinessType.DELETE)
    @RequiresPermissions("base:valve:info:cancel")
    @PostMapping("/cancel/{valveIds}")
    @ResponseBody
    public AjaxResult cancel(@PathVariable("valveIds") String valveIds) throws InterruptedException {
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = valveInfoService.cancelInfo(Convert.toStrArray(valveIds), operName);
        return AjaxResult.success(message);
    }

    /**
     * 设置阀门开度
     */
    @GetMapping("/aperture/{valveIds}")
    public String aperture(@PathVariable("valveIds") String valveIds, ModelMap mmap)
    {
        mmap.put("valveIds",valveIds);
        return prefix + "/aperture";
    }

    /**
     * 设置阀门开度
     */
    @Log(title = "设置阀门开度", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:valve:info:aperture")
    @PostMapping("/aperture")
    @ResponseBody
    public AjaxResult aperture(ValveInfo valveInfo) throws InterruptedException {
        String hex = NettyUtil.get16HexNum(valveInfo.getAperture());
        if(hex.length()<2){
            hex = "0" + hex;
        }
        String message = valveInfoService.apertureInfo(Convert.toStrArray(valveInfo.getValveIds()), hex);
        return AjaxResult.success(message);
    }

    /**
     * 限制阀门开度
     */
    @Log(title = "限制阀门开度", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:valve:info:limit")
    @PostMapping("/limit")
    @ResponseBody
    public AjaxResult limit(ValveInfo valveInfo) throws InterruptedException {
        String hex = NettyUtil.get16HexNum(valveInfo.getAperture());
        if(hex.length()<2){
            hex = "0" + hex;
        }
        String message = valveInfoService.limitInfo(Convert.toStrArray(valveInfo.getValveIds()),hex);
        return AjaxResult.success(message);
    }
    /**
     * 设置阀门开度
     */
    @GetMapping("/limit/{valveIds}")
    public String limit(@PathVariable("valveIds") String valveIds, ModelMap mmap)
    {
        mmap.put("valveIds",valveIds);
        return prefix + "/limit";
    }


    /**
     * 查询阀门信息列表
     */
    @RequiresPermissions("base:valve:info:locationlist")
    @PostMapping("/locationlist")
    @ResponseBody
    public TableDataInfo locationlist(String locationId)
    {
        startPage();
        List<ValveInfo> locationlist = valveInfoService.selectValveInfoBylocationId(locationId);
        return getDataTable(locationlist);
    }


    /**
     * 读取阀门数据
     */
    @Log(title = "读取阀门数据", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:valve:info:check")
    @PostMapping("/check/{valveIds}")
    @ResponseBody
    public AjaxResult check(@PathVariable("valveIds") String valveIds) throws InterruptedException {
        String message = valveInfoService.checkInfo(Convert.toStrArray(valveIds));
        return AjaxResult.success(message);
    }
    /**
     * 设置阀门控温上限
     */
    @GetMapping("/tempmax/{valveIds}")
    public String tempmax(@PathVariable("valveIds") String valveIds, ModelMap mmap)
    {
        mmap.put("valveIds",valveIds);
        return prefix + "/tempmax";
    }

    /**
     * 设置阀门控温上限
     */
    @Log(title = "设置阀门控温上限", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:valve:info:tempmax")
    @PostMapping("/tempmax")
    @ResponseBody
    public AjaxResult tempmax(ValveInfo valveInfo) throws InterruptedException {
        String hex = NettyUtil.get16HexNum(Math.round(Float.parseFloat(valveInfo.getTempmax())*2));
        if(hex.length()<2){
            hex = "0" + hex;
        }
        String message = valveInfoService.tempmaxInfo(Convert.toStrArray(valveInfo.getValveIds()), hex);
        return AjaxResult.success(message);
    }
    /**
     * 设置阀门开度上限
     */
    @GetMapping("/aperturemax/{valveIds}")
    public String aperturemax(@PathVariable("valveIds") String valveIds, ModelMap mmap)
    {
        mmap.put("valveIds",valveIds);
        return prefix + "/aperturemax";
    }

    /**
     * 设置阀门开度上限
     */
    @Log(title = "设置阀门开度上限", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:valve:info:aperturemax")
    @PostMapping("/aperturemax")
    @ResponseBody
    public AjaxResult aperturemax(ValveInfo valveInfo) throws InterruptedException {
        String hex = NettyUtil.get16HexNum(valveInfo.getAperturemax());
        if(hex.length()<2){
            hex = "0" + hex;
        }
        String message = valveInfoService.aperturemaxInfo(Convert.toStrArray(valveInfo.getValveIds()), hex);
        return AjaxResult.success(message);
    }
    /**
     * 设置阀门开度下限
     */
    @GetMapping("/aperturemin/{valveIds}")
    public String aperturemin(@PathVariable("valveIds") String valveIds, ModelMap mmap)
    {
        mmap.put("valveIds",valveIds);
        return prefix + "/aperturemin";
    }

    /**
     * 设置阀门开度下限
     */
    @Log(title = "设置阀门开度下限", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:valve:info:aperturemin")
    @PostMapping("/aperturemin")
    @ResponseBody
    public AjaxResult aperturemin(ValveInfo valveInfo) throws InterruptedException {
        String hex = NettyUtil.get16HexNum(valveInfo.getAperturemin());
        if(hex.length()<2){
            hex = "0" + hex;
        }
        String message = valveInfoService.apertureminInfo(Convert.toStrArray(valveInfo.getValveIds()), hex);
        return AjaxResult.success(message);
    }

    /**
     * 阀门回水温度控制使能
     */
    @Log(title = "阀门回水温度控制使能", businessType = BusinessType.DELETE)
    @RequiresPermissions("base:valve:info:outleton")
    @PostMapping("/outleton/{valveIds}")
    @ResponseBody
    public AjaxResult outleton(@PathVariable("valveIds") String valveIds) throws InterruptedException {
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = valveInfoService.outletonInfo(Convert.toStrArray(valveIds), operName);
        return AjaxResult.success(message);
    }
    /**
     * 阀门回水温度控制关闭
     */
    @Log(title = "阀门回水温度控制关闭", businessType = BusinessType.DELETE)
    @RequiresPermissions("base:valve:info:outletoff")
    @PostMapping("/outletoff/{valveIds}")
    @ResponseBody
    public AjaxResult outletoff(@PathVariable("valveIds") String valveIds) throws InterruptedException {
        String operName = ShiroUtils.getSysUser().getLoginName();
        String message = valveInfoService.outletoffInfo(Convert.toStrArray(valveIds), operName);
        return AjaxResult.success(message);
    }

    /**
     * 设置阀门回水温度
     */
    @GetMapping("/outlet/{valveIds}")
    public String outlet(@PathVariable("valveIds") String valveIds, ModelMap mmap)
    {
        mmap.put("valveIds",valveIds);
        return prefix + "/outlet";
    }

    /**
     * 设置阀门回水温度
     */
    @Log(title = "设置阀门回水温度", businessType = BusinessType.OTHER)
    @RequiresPermissions("base:valve:info:outlet")
    @PostMapping("/outlet")
    @ResponseBody
    public AjaxResult outlet(ValveInfo valveInfo) throws InterruptedException {
        String hex = NettyUtil.get16HexNum(valveInfo.getOutlet()*10);
       /* if(hex.length()<2){
            hex = "0" + hex;
        }*/

        String message = valveInfoService.outletInfo(Convert.toStrArray(valveInfo.getValveIds()), hex);
        return AjaxResult.success(message);
    }

}
